package com.woorigil.social.domain.searcher;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.woorigil.social.domain.point.PointComparator;


@Service
public class QueryAnalyzer {
	public static List<QueryPoint> queryList = new ArrayList<QueryPoint>();
	
	public void inputQuery(String query) {
		if (StringUtils.isBlank(query)) {
			return;
		}
		query = StringUtils.remove(query, " ");
		
		boolean isNew = true;
		for (QueryPoint queryPoint : queryList) {
			if (StringUtils.equals(query, queryPoint.getQuery())) {
				queryPoint.addPoint(1);
				isNew = false;
				return;
			}
		}
		if (isNew) {
			queryList.add(new QueryPoint(query, 1));
		}
	}
	
	public List<QueryPoint> findQueries(int limitSize) {
		if (queryList.size() < limitSize) {
			limitSize = queryList.size();
		}
		
		Collections.sort(queryList, new PointComparator());
		List<QueryPoint> queries = new ArrayList<QueryPoint>();
		for (int i=0; i<limitSize; i++) {
			queries.add(queryList.get(i));
		}
		
		return queries;
	}
}
